﻿var mapData;
var mapData2;
var eOnlineState;
var ePagesAndOnline;

var centerCity = "成都";
var provinceName="四川";
var mapUtil = {
	map : null,
	min : 6,
	max : 10,
	center : centerCity,
	zoom : 8,
	initType : false,
	Topologicaltype:true,
	markerInfo:new Array(),
	markers : new Array(),
	infoWindow : new Array(),
	data : new Array(),
	data_info : [],
	markerShow : -1,
	pageSize : 1,
	nowPages : 1,
	provinceIds : 0,
	cityIds : 0,
	countyIds : 0,
	listType : -1,
	listItems : 8,
	mapPly : [],
	enterpriseId : new Array(),
	obtainType : true,
	ComplexCustomOverlay:null,
	EnterpriseList:null,
	loadJScript : function(mapKey, mins, maxs, centers, zooms) {
		mapUtil.min = mins;
		mapUtil.max = maxs;
		mapUtil.center = centers;
		mapUtil.zoom = zooms;
		var script2 = document.createElement("script");
		script2.type = "text/javascript";
		script2.src = "http://api.map.baidu.com/library/AreaRestriction/1.2/src/AreaRestriction_min.js";
		document.body.appendChild(script2);
		var script = document.createElement("script");
		script.type = 'text/javascript';
		script.src = 'http://api.map.baidu.com/api?v=2.0&ak=' + mapKey
				+ '&callback=mapUtil.init';
		document.body.appendChild(script);
	},
	init : function() {
		var script = document.createElement("script");
		script.type = "text/javascript";
		script.src = "http://api.map.baidu.com/library/TrafficControl/1.4/src/TrafficControl_min.js";
		document.body.appendChild(script);
		if (BMap) {
			mapUtil.map = new BMap.Map("allmap", {
				enableMapClick:false,
				minZoom : mapUtil.min,
				maxZoom : mapUtil.max
			}); // 设置地图允许的最小/大级别
			mapUtil.map.centerAndZoom(mapUtil.center, mapUtil.zoom); // 设置地图中心位置以及地图缩放等级
			mapUtil.map.enableScrollWheelZoom(); // 启用滚轮放大缩小
			citySelsect(provinceName);
			mapUtil.initType = true; 
			mapUtil.getData();
			setInterval(function(){
				console.log('刷新');
				mapUtil.getData(mapUtil.provinceIds,mapUtil.cityIds,mapUtil.countyIds);
			}, 3600000);
			mapUtil.ComplexCustomOverlay=function(point,height, text, mouseoverText,id,HoursType,eOnlineType) {
				this._point = point;
				this._text = text;
				this._overText = mouseoverText;
				this._id = id;
				this._HoursType = HoursType;
				this._eOnlineType = eOnlineType;
				this._height = height;
			};
			mapUtil.ComplexCustomOverlay.prototype = new BMap.Overlay();
			mapUtil.ComplexCustomOverlay.prototype.initialize = function(map) {
				this._map = map;
				var contentDiv = document.createElement("div");
				contentDiv.style.width="180px";
				contentDiv.style.height = "60px";
				contentDiv.style.position = "absolute";
				contentDiv.className = 'alarm-houver';
				contentDiv.style.display = 'none';
				var div = document.createElement("div");
				var arrow = document.createElement("div");
				div.style.position = "absolute";
				div.style.backgroundColor = "#FFFFFF";
				div.style.color = "#000000";
				div.style.width = "180px";
				div.style.height = this._height + 'px';
				div.style.whiteSpace = "nowrap";
				div.style.MozUserSelect = "none";
				div.style.fontSize = '15px';
				div.style.fontWeight = 600;
				div.style.borderRadius = '4px';
				div.style.boxShadow = '1px 1px 4px #9a9999';
				div.style.padding = '5px 7px 2px 7px';
				var that = this;
				arrow.style.position = "absolute";
				arrow.style.width = "0px";
				arrow.style.height = "0px";
				arrow.style.borderLeft = '7px solid transparent';
				arrow.style.borderRight = '7px solid transparent';
				arrow.style.borderTop = '11px solid #FFFFFF';
//				var content = "<div style='text-align:center;white-space:normal;line-height:15px;width:165px;height:30px;'>"+this._text+"</div>";
				if(this._HoursType){
					if(this._eOnlineType){
						var content = "<div class='list-Covering'><span class='list-Covering-name'>"+this._text+"</span></div><div class='map-mess' ><span><img class='list-Covering-img' src='images/map_overproof.png'></span></div>";
					}else{
						var content = "<div class='list-Covering'><span class='list-Covering-name'>"+this._text+"</span></div><div class='map-mess' ><span><img class='list-Covering-img' src='images/map_overproof.png'></span><span><img class='list-Covering-img' src='images/map_dropped.png'></span></div>";
					}
					div.innerHTML = content;
				}else{
					if(this._eOnlineType){
						var content = "<div class='list-Covering'><span class='list-Covering-name'>"+this._text+"</span></div>";
						this._height = 40;
						div.style.height = '40px';
						contentDiv.style.height = "40px";
					}else{
						var content = "<div class='list-Covering'><span class='list-Covering-name'>"+this._text+"</span></div><div class='map-mess' ><span><img class='list-Covering-img' src='images/map_dropped.png'></span></div>";
					}
					div.innerHTML = content;
				}
				contentDiv.appendChild(div);
				contentDiv.appendChild(arrow);
				mapUtil.map.getPanes().labelPane.appendChild(contentDiv);
				this._div = div;
				this._arrow = arrow; 
				this._contentDiv = contentDiv;
				return contentDiv;
			}
			mapUtil.ComplexCustomOverlay.prototype.draw = function() {
				var map = this._map;
				var pixel = map.pointToOverlayPixel(this._point);
				this._contentDiv.style.left = pixel.x - 90 + "px";
				this._contentDiv.style.top = pixel.y - this._height - 22 + "px";
				/*this._div.style.marginLeft = 42 + "px";
				this._div.style.marginTop =60 + "px";*/
				this._arrow.style.marginLeft = 83 + "px";
				this._arrow.style.marginTop =this._height + "px";
			};
			mapUtil.ComplexCustomOverlay.prototype.selected = function(point) {
				var checkData2  = (point.lng =="" || point.lng==null || point.lng==undefined ||
						point.lat =="" || point.lat==null || point.lat==undefined);
				if(checkData2){
					$(document).alertmsg('warn', '该企业坐标信息不全，无法在地图上找到该企业！', {
						displayMode : 'slide',
						displayPosition : 'middlecenter',
						okName : '好的',
						title : '提示'
					});
					return;
				}
			};
			mapUtil.ComplexCustomOverlay.prototype.unselected = function() {
				if(this._div){
				}
			};
			mapUtil.ComplexCustomOverlay.prototype.show = function () {
			    if (this._div) {
			        this._contentDiv.style.display = "";
			    }
			}
			mapUtil.ComplexCustomOverlay.prototype.hide = function () {
			    if (this._div) {
			        this._contentDiv.style.display = "none";
			    }
			}
			mapUtil.ComplexCustomOverlay.prototype.addEventListener  = function(event,fun){
				if (this._contentDiv){  
					this._contentDiv['on'+event]=fun;
				}
			}
			$(".anchorBL").hide();
		}
	},
	getData : function(provinceId,cityId,countyId) {
		if(provinceId == undefined){
			provinceId="";
		}
		if(cityId == undefined){
			cityId="";
		}
		if(countyId == undefined){
			countyId="";
		}
		mapUtil.provinceIds = provinceId;
		mapUtil.cityIds = cityId;
		mapUtil.countyIds = countyId;
		$.ajax({
			url : "map/epList.shtml?specialId=" + (new Date().getTime()),
			dataType : 'json',
			data:"provinceId="+provinceId+"&cityId="+cityId+"&countyId="+countyId,
			statusCode : {
				300 : function() {
					console.log("返回状态码为：300");
					//alert('数据获取失败，请稍后再试。');
					$(document).alertmsg('warn', '数据获取失败，请稍后再试！', {
						displayMode : 'slide',
						displayPosition : 'topleft',
						okName : '好的',
						title : '提示'
					});
				}
			},
			success : function(data, textStatus, jqXHR) {
				var data = data.bean;
				console.log(data)
				if(data != null){
					if(data.length < mapUtil.listItems){
						document.getElementById('epList').style.maxHeight = (data.length*30) + 120 + 'px';
					}else{
						document.getElementById('epList').style.maxHeight = '360px';
					}
				}else{
					console.log('获取失败');
					document.getElementById('epList').style.maxHeight = '114px';
				}
				mapUtil.marker(data, 730, 650);
			},
			error : function(data) {
				console.log(data)
			}
		});
	},
	getTypeData:function(provinceId,cityId,countyId,epnoId){
		if(provinceId == undefined){
			provinceId="";
		}
		if(cityId == undefined){
			cityId="";
		}
		if(countyId == undefined){
			countyId="";
		}
		if(epnoId == undefined){
			epnoId=[];
		}
		console.log("provinceId="+provinceId+"&cityId="+cityId+"&countyId="+countyId+"&epNos="+epnoId);
		$.ajax({
			url : "map/deviceStatus.shtml?specialId="+new Date().getTime(),
			dataType : 'json',
			data:"provinceId="+provinceId+"&cityId="+cityId+"&countyId="+countyId+"&epNos="+epnoId,
			statusCode : {
				300 : function() {
					console.log("返回状态码为：300");
				}
			},
			success : function(data, textStatus, jqXHR) {
				var data = data.bean;
				if(data != null){
					eOnlineState = data;
				}else{
					eOnlineState = [];
				}
				console.log(data);
				mapUtil.pagesProcessing();
			},
			error : function(data) {
				console.log(data);
			}
		});
	},
	// 增加地图点位
	marker : function(point, widths, heights) {
		mapData = point;
		mapData2 = point;
		if (mapUtil.initType) {
				mapUtil.pagesinit();
				var queryTxt = document.getElementById('queryTxt').value;
				if(point != null ){
					mapUtil.pageSize =  Math.ceil(point.length / mapUtil.listItems);
					if(mapUtil.pageSize == 0){
						mapUtil.pageSize = 1;
					}
				}else{
					mapUtil.pageSize =  1;
				}
				mapUtil.pageType();
				document.getElementById('pagesSize').innerHTML = mapUtil.pageSize;
				if(queryTxt == ''){
					mapUtil.pages(point);
				}else{
					mapUtil.dataQuery(mapData2);
				}
			}
	},
	removeMarker : function() {
		if (mapUtil.data_info.length != 0) {
			for (var i = 0; i < mapUtil.data_info.length; i++) {
				mapUtil.map
						.removeOverlay(mapUtil.markers[mapUtil.data_info[i]]);
				mapUtil.map
				.removeOverlay(mapUtil.markerInfo[mapUtil.data_info[i]]);
			}
			mapUtil.data_info = [];
			mapUtil.markers = [];
			mapUtil.markerShow = -1;
		}
	},
	pagesinit : function(){
		mapUtil.removeMarker();
		mapUtil.markerInfo = [];
		mapUtil.infoWindow = [];
		mapUtil.nowPages = 1; 
		mapUtil.pageSize = 1;
		mapUtil.listType = -1;
		document.getElementById('NowPages').innerHTML = mapUtil.nowPages;
		$('.portBtn').hide();
		$(".map-right-top").hide();
	},
	dataQuery : function(){
		mapUtil.pagesinit();
		var data = mapData;
		var afterData = new Array();
		var queryTxt = document.getElementById('queryTxt').value;
		if(queryTxt != ''){
			for(var i = 0;data!=null && i<data.length;i++){
				var bool = data[i][2].indexOf(queryTxt);
				if(bool != -1){
					afterData.push(data[i]);
				}
			}
			if(afterData != null){
				mapUtil.pageSize =  Math.ceil(afterData.length / 10);
			}
			if(mapUtil.pageSize == 0){
				mapUtil.pageSize = 1;
			}
			mapData2 = afterData;
			mapUtil.pageType();
			mapUtil.pages(mapData2);
			document.getElementById('pagesSize').innerHTML = mapUtil.pageSize;
		}else{
			mapUtil.getData(mapUtil.provinceIds,mapUtil.cityIds,mapUtil.countyIds);
		}
	},
	panTo:function(value){
		console.log(value);
		if(value != '请选择地市' && value !=""){
			mapUtil.mapCity(value);
		}else{
			mapUtil.removePolygon();
			mapUtil.map.centerAndZoom(provinceName,'6');
		}
	},
	panToCity:function(value,city){
		console.log(value);
		mapUtil.mapCity(value);
	},
	pageType:function(){
		if(mapUtil.nowPages == 1){
			if(mapUtil.pageSize == 1){
				document.getElementById("lastpage").className = 'disabled';
				document.getElementById("nextpage").className = 'disabled';
			}else{
				document.getElementById("lastpage").className = 'disabled';
				document.getElementById("nextpage").className = ' ';
			}
		}else if(mapUtil.nowPages == mapUtil.pageSize){
			document.getElementById("nextpage").className = 'disabled';
			document.getElementById("lastpage").className = ' ';
		}else{
			document.getElementById("lastpage").className = ' ';
			document.getElementById("nextpage").className = ' ';
		}
	},
	pagesLast:function(){//上一页
		if(mapUtil.nowPages > 1){
			mapUtil.map.centerAndZoom(mapUtil.center, 6);
    		--mapUtil.nowPages;
    		document.getElementById("NowPages").innerHTML = mapUtil.nowPages;
			mapUtil.pageType();
			mapUtil.pages(mapData2);
		}
	},
	pagesNext:function(){//下一页
		if(mapUtil.nowPages < mapUtil.pageSize){
			mapUtil.map.centerAndZoom(mapUtil.center, 6);
			++mapUtil.nowPages;
			document.getElementById("NowPages").innerHTML = mapUtil.nowPages;
			mapUtil.pageType();
			mapUtil.pages(mapData2);
		}
	},
	pagesNum : function(Num,data){
		var Numbers = mapUtil.listItems;
		if(data != null){
			if ((data.length - Num) < mapUtil.listItems) {
				Numbers = data.length - Num;
			} else {
				Numbers = mapUtil.listItems;
			}
		}else{
			Numbers = 0;
		}
		return Numbers;
	},
	pages:function(data){
		$("#portBtn").hide();
		$(".map-right-top").hide();
		mapUtil.enterpriseId = [];
		var Num = (mapUtil.nowPages - 1) * mapUtil.listItems;
		var Numbers = mapUtil.pagesNum(Num,data);
		var point = data;
		ePagesAndOnline = data;
		for(var i = Num;point!=null && i <(Num + Numbers); i++){
			mapUtil.enterpriseId.push(point[i][4]);
		}
		if(point != null ){
			if(mapUtil.obtainType){
				console.log(mapUtil.enterpriseId);
				mapUtil.getTypeData(mapUtil.provinceIds,mapUtil.cityIds,mapUtil.countyIds,mapUtil.enterpriseId);
				setInterval(function(){
					console.log('刷新');
					mapUtil.getTypeData(mapUtil.provinceIds,mapUtil.cityIds,mapUtil.countyIds,mapUtil.enterpriseId);
				}, 300000);
				mapUtil.obtainType = false;
			}else{
				console.log(mapUtil.enterpriseId);
				mapUtil.getTypeData(mapUtil.provinceIds,mapUtil.cityIds,mapUtil.countyIds,mapUtil.enterpriseId);
			}
		}else{
			ePagesAndOnline = [];
			mapUtil.pagesProcessing();
		}
	},
	pagesProcessing : function(){
		$.CurrentNavtab.find("#epLiList").html("");
		mapUtil.removeMarker();
		var Num = (mapUtil.nowPages - 1) * mapUtil.listItems;
		var Numbers = mapUtil.pagesNum(Num,ePagesAndOnline);
		var point = ePagesAndOnline;
		document.getElementById('epList').style.maxHeight = (Numbers*30) + 120 + 'px';
		var markerIconNums = 1;
		for(var i = Num;point!=null && i <(Num + Numbers); i++){
			var pointIcon = new BMap.Icon("images/map/marker_"+markerIconNums+".png", new BMap.Size(18, 25));
			pointIcon.setImageSize(new BMap.Size(18, 25));
			mapUtil.data_info.push(i);
			var checkData  =( point[i][0]=="" || point[i][0]==undefined || point[i][0]=="null" || point[i][1]=="" || point[i][1]==undefined || point[i][1]=="null");
			var alarmData =point[i][5];
			var alarmType = false;
			if(alarmData != null && alarmData.length != 0){
				for(var n=0;n<alarmData.length;n++){
					if(alarmData[n].hasOwnProperty("dayalarm")){
						alarmType = true;
						break;	
					}
				}
			}
			
			var gettype = Object.prototype.toString;
			var eOnlineType = true;//该企业是否有掉线设备
			for(var j = 0;j<eOnlineState.length;j++){
				if(point[i][4] == eOnlineState[j].ep_no){
					eOnlineType = false;
					break;
				}
			}
			mapUtil.markers[i] = new mapUtil.ComplexCustomOverlay(new BMap.Point(
					point[i][0], point[i][1]),60, point[i][2], point[i][3],i,alarmType,eOnlineType); // 创建标注
			mapUtil.markerInfo[i] = new BMap.Marker(new BMap.Point(
					point[i][0], point[i][1]),{
				icon: pointIcon
			});//创建marker坐标
			
			mapUtil.map.addOverlay(mapUtil.markerInfo[i]);
			var li = document.createElement("li");
			if(alarmType){
				if(eOnlineType){
					li.innerHTML="<div id='list"+i+"' class='list-li-left'><span><img class='marker-Icon' src='images/map/marker_"+markerIconNums+".png'></span><span class='list-li-title1'>"+point[i][2]+"</span><span><img class='list-li-img' src='images/map_overproof.png'></span></div>";
				}else{
					li.innerHTML="<div id='list"+i+"'  class='list-li-left'><span><img class='marker-Icon' src='images/map/marker_"+markerIconNums+".png'></span><span></span><span class='list-li-title2'>"+point[i][2]+"</span><span><img class='list-li-img' src='images/map_overproof.png'></span><span><img class='list-li-img' src='images/map_dropped.png'></span></div>";
				}
			}else{
				if(eOnlineType){
					li.innerHTML="<div id='list"+i+"'  class='list-li-left'><span><img class='marker-Icon' src='images/map/marker_"+markerIconNums+".png'></span><span></span><span class='list-li-title3'>"+point[i][2]+"</span></div>";
				}else{
					li.innerHTML="<div id='list"+i+"'  class='list-li-left'><span><img class='marker-Icon' src='images/map/marker_"+markerIconNums+".png'></span><span></span><span class='list-li-title1'>"+point[i][2]+"</span><span><img class='list-li-img' src='images/map_dropped.png'></span></div>";
				}
			}
			li.addEventListener(
					"click",
					(function(e) {
						// 闭包
						return function() {
							console.log(mapUtil.markers);
							
			 				mapUtil.markers[e].selected(new BMap.Point(
										point[e][0], point[e][1]));
			 				if(mapUtil.markerShow != -1){
			 					mapUtil.markers[mapUtil.markerShow].hide();
			 				}
			 				mapUtil.map.panTo(new BMap.Point(point[e][0], point[e][1]));
			 				console.log(mapUtil.markers+'\t'+mapUtil.markerShow);
			 				mapUtil.markers[e].show();
			 				mapUtil.markerShow = e;
			 				if(mapUtil.listType != -1){
			 					console.log(mapUtil.listType);
			 					$("#list"+mapUtil.listType+"").removeClass("list-bg");
			 				}
			 				$("#list"+e+"").addClass("list-bg"); 
			 				document.getElementById('portTitle').innerHTML = point[e][2];
		 					mapUtil.listType = e;
			 				$(".map-right-top").show();
			 				HoursData(point[e][5]);
			 				OnlineState(point[e][4]);
			 				
						}
					})(i));
			$.CurrentNavtab.find("#epLiList").append(li);
			
			/* var title = point[i][2]; var content="<hr><div
			 * id='mapContent'
			 * style='width:700px;height:600px;'>"+point[i][3]+"</div>";
			 * mapUtil.infoWindow[i] = new BMap.InfoWindow(content, { width :
			 * widths, // 信息窗口宽度 height : heights, // 信息窗口高度 title : title, //
			 * 信息窗口标题 enableMessage : false, offset:new BMap.Size(0, -25) //
			 * 设置允许信息窗发送短息 });
			 */
			if(!checkData){	
				
				mapUtil.map.addOverlay(mapUtil.markers[i]); // 将标注添加到地图中
				mapUtil.markerInfo[i]
				.addEventListener(//自定义覆盖物
						"click",
						(function(e) {
							// 闭包
							return function() {
								if(mapUtil.markerShow != -1){
				 					mapUtil.markers[mapUtil.markerShow].hide();
				 				}
				 				mapUtil.markers[e].show();
				 				mapUtil.markerShow = e;
				 				if(mapUtil.listType != -1){
				 					console.log(mapUtil.listType);
				 					$("#list"+mapUtil.listType+"").removeClass("list-bg");
				 				}
				 				$("#list"+e+"").addClass("list-bg"); 
				 				mapUtil.listType = e;
							}
						})(i));
				mapUtil.markers[i]
				.addEventListener(//marker点
						"click",
						(function(e) {
							// 闭包
							return function() {
								$(document).navtab({id:'epDetail', url:'enterprise/detail.shtml?epNo='+ point[e][4], title:point[e][2]});
							}
						})(i));
			}
			if(i == Num){
				mapUtil.markers[Num].selected(new BMap.Point(
							point[Num][0], point[Num][1]));
				if(mapUtil.markerShow != -1){
					mapUtil.markers[mapUtil.markerShow].hide();
				}
				if(mapUtil.listType != -1){
 					$("#list"+mapUtil.listType+"").removeClass("list-bg");
 				}
 				$("#list"+Num+"").addClass("list-bg"); 
 				mapUtil.listType = Num;
				mapUtil.markers[Num].show();
				mapUtil.markerShow = Num;
			}	
			markerIconNums++;
		}
		$('#pagination1').show();
	},
	mapCity:function(value){
		var bdary = new BMap.Boundary();  
	    bdary.get(value, function(rs){       //获取行政区域  
	    	var Num = rs.boundaries.length;
	        var datas = [];
	        for(var i = 0;i<Num;i++){
	        	var hours = rs.boundaries[i].split(";");
		        for(var i = 0; i < hours.length; i++){
		        	datas.push(hours[i].split(","));
		        }
	        }
	        mapUtil.removePolygon();
	        
	        for(var i = 0;i<Num;i++){
	        	 mapUtil.mapPly[i] = new BMap.Polygon(rs.boundaries[i], {strokeWeight: 1,strokeStyle: "dashed", strokeColor: "#0000ff"}); //建立多边形覆盖物  
	        	 mapUtil.mapPly[i].setFillColor('#4db7ef');
		         mapUtil.mapPly[i].setFillOpacity(0.2);
		         mapUtil.mapPly[i].enableMassClear(true);
		         mapUtil.map.addOverlay(mapUtil.mapPly[i]);  //添加覆盖物
	        }
	        setZoom(datas);
	    });
	},
	removePolygon:function(){
		if(mapUtil.mapPly.length != 0){
			for(var i = 0 ;i<mapUtil.mapPly.length;i++){
				mapUtil.map.removeOverlay(mapUtil.mapPly[i]);
			}
			mapUtil.mapPly = [];
		}
	}
};

function setZoom(points){  
	console.log(points[0])
    if(points.length>0){  
        var maxLng = points[0][0];  
        var minLng = points[0][0];  
        var maxLat = points[0][1];  
        var minLat = points[0][1];  
        var res;  
        for (var i = points.length - 1; i >= 0; i--) {  
            res = points[i];  
            if(res[0] > maxLng) maxLng =res[0];  
            if(res[0] < minLng) minLng =res[0];  
            if(res[1] > maxLat) maxLat =res[1];  
            if(res[1] < minLat) minLat =res[1];  
        };  
        var cenLng =(parseFloat(maxLng)+parseFloat(minLng))/2;  
        var cenLat = (parseFloat(maxLat)+parseFloat(minLat))/2;  
        var zoom = getZoom(maxLng, minLng, maxLat, minLat); 
        console.log(maxLng+'\t'+minLng+'\t'+maxLat+'\t'+minLat+'\t'+zoom);
        mapUtil.map.centerAndZoom(new BMap.Point(cenLng,cenLat), zoom);    
    }else{  
        //没有坐标，显示全中国  
    	mapUtil.map.centerAndZoom(new BMap.Point(103.388611,35.563611), 5);    
    }   
} 
function getZoom (maxLng, minLng, maxLat, minLat) {  
    var zoom = ["50","100","200","500","1000","2000","5000","10000","20000","25000","50000","100000","200000","500000","1000000","2000000"]//级别18到3。  
    var pointA = new BMap.Point(maxLng,maxLat);  // 创建点坐标A  
    var pointB = new BMap.Point(minLng,minLat);  // 创建点坐标B  
    var distance = mapUtil.map.getDistance(pointA,pointB).toFixed(1);  //获取两点距离,保留小数点后两位  
    console.log(distance)
    for (var i = 0,zoomLen = zoom.length; i < zoomLen; i++) {  
        if(zoom[i] - distance > 0){  
            return 18-i+3;//之所以会多3，是因为地图范围常常是比例尺距离的10倍以上。所以级别会增加3。  
        }  
    };  
}

//添加省边界线
function citySelsect(cityName){
	var bdary = new BMap.Boundary();  
    bdary.get(cityName, function(rs){       //获取行政区域  
    	/*mapUtil.map.clearOverlays();*/        //清除地图覆盖物  
        var count = rs.boundaries.length; //行政区域的点有多少个  
        for(var i = 0; i < count; i++){  
            var ply = new BMap.Polygon(rs.boundaries[i], {strokeWeight: 2, strokeColor: "#ff0000"}); //建立多边形覆盖物  
            ply.setFillColor('none'); 
            ply.disableMassClear();
            mapUtil.map.addOverlay(ply);  //添加覆盖物
        }                  
    });
}

function currentDate(){//获取当前时间
	var date = new Date();
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
	if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
	var currentdate = date.getFullYear() + '-' + month + '-' + strDate;
	return currentdate;
}

function HoursData(data){//小时数据
	console.log('jinru');
	$.CurrentNavtab.find("#alarmdata").html("");
	if(data != null && data.length != 0){
		var sub = 0;
		for(var i=0;i<data.length;i++){
			sub = i;
			var dayalarm = data[i].dayalarm;
				console.log(dayalarm);
				var div = document.createElement("div");
				div.className = 'panel panel-default';
				div.innerHTML = '<div class="panel-heading" style="margin-left:0;margin-right:0;padding:10px 2px;background-color:#f3f8fc;"><h1 class="panel-title" style="font-size:14px;">'
					+data[i].machine_name+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+currentDate()
					+'</h1></div>'
					+'<div class="table-responsive"><table class="table table-hover" style="margin:0;width:100%;">'
					+'<thead><tr><th>污染项目</th><th>实测值</th><th>折算值</th><th>标准值</th><th>超标时间段</th></tr></thead><tbody id="panel'+i+'" style="height:auto;max-height:260px;overflow:auto;"></tbody></table></div>';
				$.CurrentNavtab.find("#alarmdata").append(div);
				if(dayalarm && dayalarm != undefined){
					for(var j=0;j<dayalarm.length;j++){
						console.log(dayalarm[j].codename);
						var tr = document.createElement("tr");
						tr.innerHTML = '<td>'+dayalarm[j].codename+'</td><td>'+dayalarm[j].avg+'</td><td>'+dayalarm[j].zsavg+'</td><td>'+dayalarm[j].standardmax+'</td><td>'+dayalarm[j].hour+'时</td>';
						$.CurrentNavtab.find("#panel"+sub+"").append(tr);
					}
				}else{
					var tr = document.createElement("tr");
					tr.innerHTML = '<td colspan="4">未发现小时超标</td>';
					$.CurrentNavtab.find("#panel"+sub+"").append(tr);
				}
		}
	}else{
		var div = document.createElement("div");
		div.className = 'panelDiv';
		div.innerHTML = '无机组监测信息';
		$.CurrentNavtab.find("#alarmdata").append(div);
	}
}

function OnlineState(id){//设备离线状态
	var eId = false;
	console.log(id);
	$.CurrentNavtab.find("#eOnlineStates").html("");
	if(eOnlineState.length != 0){
		var div = document.createElement("div");
		div.className = 'table-responsive';
		div.innerHTML = '<table class="table" ><thead><tr><th>设备名</th><th>监测点位</th><th>设备状态</th></tr></thead><tbody id="eStates"></tbody></table>';
		$.CurrentNavtab.find("#eOnlineStates").append(div);
		for(i = 0;i<eOnlineState.length;i++){
			if(id == eOnlineState[i].ep_no){
				console.log((eOnlineState[i].online_status == 0)+'\t'+(eOnlineState[i].online_status == '0'));
				if(eOnlineState[i].online_status == 0){
					var online_status = '离线';
				}
				eId = true;
				var tBody = document.createElement("tr");
				tBody.innerHTML = '<td>'+eOnlineState[i].device_name+'</td><td>'+eOnlineState[i].video_point_name+'</td><td>'+online_status+'</td>';
				$.CurrentNavtab.find("#eStates").append(tBody);
			}
		}
		console.log(eId);
		if(eId == false){
			$.CurrentNavtab.find("#eOnlineStates").html("");
			console.log('进入判断');
			var div = document.createElement("div");
			div.className = 'panelDiv';
			div.innerHTML = '无离线设备';
			$.CurrentNavtab.find("#eOnlineStates").append(div);
		}
	}else{
		var div = document.createElement("div");
		div.className = 'panelDiv';
		div.innerHTML = '无离线设备';
		$.CurrentNavtab.find("#eOnlineStates").append(div);
	}
}

//右上角button的显示和隐藏
function GuidemapHide(){
	mapUtil.Topologicaltype = false;
	$('.map-right-top').hide();
	$('#portBtn').show();
}
function GuidemapShow(){
	mapUtil.Topologicaltype = true;
	$('#portBtn').hide();
	$('.map-right-top').show();
}

